#!/bin/bash

###################
# Interfaces      #
###################
EXT_NIC="eth0"
INT_NIC="eth1"

#----------------------------------------------------------------------------------------
total_clients=0
total_active_groups=0
total_sub=0

group_definitions_name=()
group_definitions_down=()
group_definitions_up=()
group_definitions_ceil=()
group_definitions_aggr=()
group_definitions_prio=()
group_definitions_client_count=()
group_definitions_sub_count=()
group_definitions_active=()

group_definitions_totalband=0
group_definitions_groupband=()
group_definitions_subband=()

#################################################
# get all clients for certain group             #
#################################################
function collect_clients {
	echo "collecting $1 at Group index $2 "

	if [ -f $1.group ];
	then
		#sort and deduplicate
	        cat $1.group|sort -n|uniq

		while read line
        	do
			total_clients=$(($total_clients+1))
			#clients per group
			group_definitions_client_count[$2]=$((group_definitions_client_count[$2]+1))
        	done <$1.group
	
		group_definitions_sub_count[$2]=$(($((${group_definitions_client_count[$2]}/${group_definitions_aggr[$2]}))+1))
		group_definitions_active[$2]=1
		echo "-------------------------------------------------------------------------------------"
		echo "Found ${group_definitions_client_count[$2]} clients in ${group_definitions_name[$2]}"
		echo "total ${group_definitions_sub_count[$2]} sub-groups"
		echo "Found ${group_definitions_client_count[$2]} clients in ${group_definitions_name[$2]}"
		echo "-------------------------------------------------------------------------------------"
		total_active_groups=$(($total_active_groups+1))
		total_sub=$((${total_sub}+${group_definitions_sub_count[$2]}))
		echo "TOTAL SUB is ${total_sub}"
	else
   		echo -e "${RED}#### ERROR! #### File $1.group does not exist but is defined in group.definitions${NONE}"
		group_definitions_active[$2]=0
	fi

	#create_leaf_classes ${group_definitions_name[$2]} ${group_definitions_sub_count[$2]}
	
}

##########################################################################
# load groups from definition file and compute traffic values like ceil  #
##########################################################################
function prepare_group_definitions {

        while read line
        do
                #take line and split it into array, first value is main! (aka group,client..etc)
                arrl=($(echo $line | tr " " "\n"))
		if [ -z "$line" ]; then
			echo "EMPTY LINE"
		else 
			case "${arrl[0]}" in
                '#'*)  
                        ;;
                *)      echo "---------------------------------------------------------------"
			echo "found GROUP called ${arrl[0]} | download $((arrl[1] / 1024)) Mbs ON INDEX ${#group_definitions_name[*]}"
                        group_definitions_name[$[${#group_definitions_name[*]}]]=${arrl[0]}
                        group_definitions_down[$[${#group_definitions_down[*]}]]=${arrl[1]}
                        group_definitions_up[$[${#group_definitions_up[*]}]]=${arrl[2]}
                        #group_definitions_ceil=$arrl[3] DO NOT SET, automatic computation
                        group_definitions_aggr[$[${#group_definitions_aggr[*]}]]=${arrl[3]}
                        group_definitions_prio[$[${#group_definitions_prio[*]}]]=${arrl[4]}
			group_definitions_client_count[$[${#group_definitions_client_count[*]}]]=0
			
			echo "COLLECT CLIENTS ${arrl[0]} $[${#group_definitions_name[*]}-1]"
			collect_clients ${arrl[0]} $[${#group_definitions_name[*]}-1]
			echo "---------------------------------------------------------------"
                        ;;
                esac
		fi

        done <group.definitions

	echo "END OF DEFINITIONS"
	stats
}

################################################
# Shaping and tree organization                #
################################################
function tc_init {
	echo "TC_INIT>................................"
        #get group count
        prepare_group_definitions
        #local master_index=$group_definitions_name[@]
	local groupid=0

        #delete old qdisc, create new root qdisc on external NIC
        echo "$TC qdisc del dev $EXT_NIC root"
        echo "$TC qdisc add dev $EXT_NIC root handle 1:0 htb " #default $group_definitions_name[@]"
        echo "$TC class add dev $EXT_NIC parent 1:0 classid 1:1 htb rate $group_definitions_totalband"
	
	echo "=================================================================================================="
        #for i in "${master_groups_name[@]}"
        #do
        #        echo "i: ${i}"
        #        #$TC class add dev $EXT_NIC parent 1:1 classid 1:${groupid} htb rate ${master_total_rate} ceil ${group_definitions_totalband}
        #        #master_index=$((master_index + 1))
#		$groupid=$(($groupid+10))
#		echo "GROUPID is ${groupid}"
#        done
	echo "count: ${#group_definitions_name[*]}" 
	for ((f=0; f<${#group_definitions_name[*]}; f++));
	do
		echo "INDEX: $f NAME: ${group_definitions_name[$f]}"
		if [[ ${group_definitions_active[$f]} -eq 1 ]];
		then
			echo ${group_definitions_name[$f]}
		 	groupid=$(($groupid+10))
			echo "class add dev $EXT_NIC parent 1:1 classid 1:${groupid} htb rate ${master_total_rate} ceil ${group_definitions_totalband}Kbit"
		else
			echo ${group_definitions_name[$f]}
			echo "group not active... SKIP"
		fi
	done
}


